﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.TCP;
using WFNetLib.MyControls;
using WFNetLib.Log;

namespace gprs服务器测试
{
    public partial class Form1 : Form
    {
        TCPAsyncServer tcpAsyncServer = null;
        public Form1()
        {
            InitializeComponent();
        }
        public object SaveDataProcessCallbackProc(byte[] tempbuffer,ref byte[] buffer,ref int dataOffset, int length)
        {
            //判断读取的字节数+缓冲区已有字节数是否超过缓冲区总大小
            if (length + dataOffset > buffer.Length)
            {
                Array.Resize<Byte>(ref buffer, buffer.Length + 1024);
            }
            //将新读取的数据拷贝到缓冲区
            Array.Copy(tempbuffer, 0, buffer, dataOffset, length);
            //修改"数据实际长度"
            dataOffset += length;
            string strRx=Encoding.Default.GetString(buffer,0,dataOffset);
            if (strRx.IndexOf("#") == -1)
                return null;

            //获得了完整的数据包            
            Array.Copy(buffer, strRx.Length, buffer, 0, dataOffset - (strRx.Length));
            dataOffset -= (strRx.Length);//缓冲区实际数据长度减去一个完整封包长度
            return strRx;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            tcpAsyncServer = new TCPAsyncServer();
            tcpAsyncServer.ErrorServerEvent += new TCPErrorEvent(tcpAsyncServer_ErrorServer);
            tcpAsyncServer.AcceptServerEvent += new TCPAcceptEvent(tcpAsyncServer_AcceptServer);
            tcpAsyncServer.DisconnectServerEvent += new TCPDisconnectEvent(tcpAsyncServer_DisconnectServer);
            tcpAsyncServer.ReceiveServerEvent += new TCPReceiveEvent(tcpAsyncServer_ReceiveServerEvent);
            //tcpAsyncServer.SaveDataProcessCallback = new SaveDataProcessCallbackDelegate(SaveDataProcessCallbackProc);
            tcpAsyncServer.TCPServerPort = 8090;
            tcpAsyncServer.Start();
        }
        private void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                textBox1.AppendText("收到信息:" + e.netPacket.ToString() + "\r\n");
                string str = e.Client.clientEndPoint.Address.ToString() + ":" + e.Client.clientEndPoint.Port.ToString();
                str = str+"------>"+e.netPacket.ToString();
                tcpAsyncServer.Send(e.Client, Encoding.Default.GetBytes(str));
            }));
        }
        private void tcpAsyncServer_ErrorServer(object sender, ErrorServerEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                textBox1.AppendText(e.Message+e.Error.Message + "\r\n");
                textBox1.AppendText(e.Error.StackTrace + "\r\n");

            }));
        }
        private void tcpAsyncServer_AcceptServer(object sender, AcceptServerEventArgs e)
        {
            //e.isCancel = true;
            this.Invoke((EventHandler)(delegate
            {
                wfComboBoxItem c = new wfComboBoxItem(e.Client.key, e.Client);
                int i = listBox1.Items.Add(c);
                e.Client.SaveDataProcessCallback = new SaveDataProcessCallbackDelegate(SaveDataProcessCallbackProc);
                //toolStripComboBox1.SelectedIndex = i;
            }));
        }
        private void tcpAsyncServer_DisconnectServer(object sender, DisconnectEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                foreach (object o in listBox1.Items)
                {
                    if (o.ToString() == e.Client.key)
                    {
                        listBox1.Items.Remove(o);
                        break;
                    }
                }
            }));
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 20000)
            {
                TextLog.AddTextLog(textBox1.Text);
                textBox1.Clear();
            }
        }
    }
}
